/*
 * @Descripttion:
 * @Author: YuZhenJing
 * @Date: 2020-02-10 18:06:15
 * @LastEditors: YuZhenJing
 * @LastEditTime: 2020-05-14 17:38:31
 */
import axios from 'axios'
import qs from 'qs'
import { MessageBox } from 'element-ui'
import AsmClient from 'asm-client'
import router from '../router'

const AxiosRule = AsmClient.AxiosRule

/**
 * 实例化
 */
const service = axios.create({
  baseURL: '/asmbody/',
  timeout: '5000',
  headers: { 'Cache-Control': 'no-store' }
})

/**
 * 提示函数
 */
const dialog = (msg, type) => {
  MessageBox.alert(msg, '提示', {
    type: type ? type : 'warning',
    confirmButtonText: '确定'
  })
}

/**
 * 路径重定向
 */
const redirectUrl = url => {
  router.replace({ path: url, replace: true })
}

/**
 * 设置config
 */
const configOptionFn = (config, option) => {
  if (option) {
    if (option.root == true) {
      config.baseURL = ''
    }
  }
}

/**
 * 请求配置
 */
service.interceptors.request.use(
  config => {
    return AxiosRule.interceptorsRequestUse(config)
  },
  err => {
    return Promise.reject(err)
  }
)

/**
 * 响应配置
 */
service.interceptors.response.use(
  res => {
    return AxiosRule.interceptorsResponseUse(res, redirectUrl, dialog)
  },
  err => {
    return AxiosRule.interceptorsResponseUseErr(err, redirectUrl, dialog)
  }
)

export const postRequest = (url, params, configOption) => {
  let config = {
    method: 'post',
    url: `${url}`,
    data: qs.stringify(params),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }
  configOptionFn(config, configOption)
  return service(config)
}

export const postRequestJson = (url, params, configOption) => {
  let config = {
    method: 'post',
    url: `${url}`,
    data: JSON.stringify(params),
    headers: {
      'Content-Type': 'application/json'
    }
  }
  configOptionFn(config, configOption)
  return service(config)
}

export const uploadFileRequest = (url, params, configOption) => {
  let config = {
    method: 'post',
    url: `${url}`,
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }
  configOptionFn(config, configOption)
  return service(config)
}

export const putRequest = (url, params, configOption) => {
  let config = {
    method: 'put',
    url: `${url}`,
    data: qs.stringify(params),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }
  configOptionFn(config, configOption)
  return service(config)
}

export const putRequestJson = (url, params, configOption) => {
  let config = {
    method: 'put',
    url: `${url}`,
    data: JSON.stringify(params),
    headers: {
      'Content-Type': 'application/json'
    }
  }
  configOptionFn(config, configOption)
  return service(config)
}

export const deleteRequest = (url, params, configOption) => {
  let config = {
    method: 'delete',
    url: `${url}`,
    data: params
  }
  configOptionFn(config, configOption)
  return service(config)
}

export const getRequest = (url, params, configOption) => {
  let config = {
    method: 'get',
    url: `${url}`,
    params: params
  }
  configOptionFn(config, configOption)
  return service(config)
}

const diyAxios = {
  install: function(Vue) {
    const DiyRequest = new Vue({
      methods: {
        post: postRequest,
        postJson: postRequestJson,
        uploadFile: uploadFileRequest,
        put: putRequest,
        putJson: putRequestJson,
        delete: deleteRequest,
        get: getRequest
      }
    })
    Vue.prototype.$html = DiyRequest
  }
}

export default diyAxios
